package com.managedBean.user;

import java.io.Serializable;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.map.MarkerDragEvent;
import org.primefaces.event.map.PointSelectEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

import com.controller.user.MyAccountController;
import com.dto.GooglemapsDTO;
import com.dto.merchant.UserDTO;
import com.managedBean.registration.LoginManagedBean;
import com.utils.JsfUtils;

@ViewScoped
@ManagedBean(name = "myAccount")
public class MyAccountManagedBean implements Serializable {

	private static final long serialVersionUID = 3000700067041476355L;
	private UserDTO myAccountDTO;
	@ManagedProperty(value = "#{myAccountController}")
	private MyAccountController myAccountController;
	LoginManagedBean loginBean;

	// G Map code starts here
	private MapModel emptyModel;
	private String title;
	private double lat;
	private double lng;
	private boolean updateFlag;
	private String latLang;
	@PostConstruct
	public void init() {
		loginBean = (LoginManagedBean) JsfUtils
				.getBeanInstanceFromFacesContext("login");
		setMyAccountDTO(loginBean.getUserDTO());
		latLang="17.494974,78.334493";
		// GMap Code
		emptyModel = new DefaultMapModel();
		if (null != myAccountDTO.getGooglemapsDTO()) {
			GooglemapsDTO dto = new GooglemapsDTO(myAccountDTO
					.getGooglemapsDTO().getLat(), myAccountDTO
					.getGooglemapsDTO().getLang(), myAccountDTO);
			marker = new Marker(new LatLng(new Double(dto.getLat()),
					new Double(dto.getLang())), title);
			marker.setDraggable(true);
			emptyModel.addOverlay(marker);
			updateFlag=true;
			latLang=dto.getLat()+","+dto.getLang();
			address=myAccountDTO.getArea();
		}
	}

	public void updateDetails() {
		try {
			myAccountController.updateDetails(myAccountDTO);
			updateLoginBean();
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage("Updated Successfully"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void updateLoginBean() {
		loginBean.setUserDTO(getMyAccountDTO());
	}

	Marker marker;

	public void addMarker(ActionEvent actionEvent) {
		try {
			myAccountDTO.setGooglemapsDTO(new GooglemapsDTO(
					String.valueOf(lat), String.valueOf(lng), myAccountDTO));
			myAccountController.updateGooglemap(myAccountDTO, true);
			emptyModel = new DefaultMapModel();
			marker = new Marker(new LatLng(lat, lng), title);
			emptyModel.addOverlay(marker);
			for (Marker marker : emptyModel.getMarkers()) {
				marker.setDraggable(true);
			}
			addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO,
					"Address Added", "Lat:" + lat + ", Lng:" + lng));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public  void  pointSelected (PointSelectEvent event) {  
		LatLng latituteLongitude = event.getLatLng();
           
        FacesContext.getCurrentInstance ().addMessage (
            null ,
            new  FacesMessage (
                FacesMessage.SEVERITY_INFO, 
                "Point selected" ,
                "Lat:"  + latituteLongitude.getLat () + "Long:"  + latituteLongitude.getLng ()
            )
        );  
    }  
	public void onMarkerDrag(MarkerDragEvent event) {
		marker = event.getMarker();
		addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO,
				"Marker Dragged", "Lat:" + marker.getLatlng().getLat()
						+ ", Lng:" + marker.getLatlng().getLng()));
	}

	public void updateMarker()  {
		try {
		myAccountDTO.setGooglemapsDTO(new GooglemapsDTO(String.valueOf(marker
				.getLatlng().getLat()), String.valueOf(marker.getLatlng()
				.getLng()), myAccountDTO));
		myAccountController.updateGooglemap(myAccountDTO, false);
		addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO,
				"Marker Updated", "Lat:" + marker.getLatlng().getLat()
						+ ", Lng:" + marker.getLatlng().getLng()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private String address;
	public void getCordinates() {
		   FacesContext context = FacesContext.getCurrentInstance();
		   Map<String, String> parameterMap = context.getExternalContext().getRequestParameterMap();
		   double latitude = Double.parseDouble((String) parameterMap.get("latitude"));
		   double longitude = Double.parseDouble((String) parameterMap.get("longitude"));
		   System.out.println("latitude "+latitude+"longitude "+longitude);
		   latLang=latitude+","+longitude;
		   emptyModel = new DefaultMapModel();
		   marker = new Marker(new LatLng(latitude, longitude), title);
		   marker.setDraggable(true);
		   emptyModel.addOverlay(marker);
	}
	
	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the myAccountUserDTO
	 */
	public UserDTO getMyAccountDTO() {
		return myAccountDTO;
	}

	/**
	 * @param myAccountUserDTO
	 *            the myAccountUserDTO to set
	 */
	public void setMyAccountDTO(UserDTO myAccountDTO) {
		this.myAccountDTO = myAccountDTO;
	}

	/**
	 * @return the myAccountController
	 */
	public MyAccountController getMyAccountController() {
		return myAccountController;
	}

	/**
	 * @param myAccountController
	 *            the myAccountController to set
	 */
	public void setMyAccountController(MyAccountController myAccountController) {
		this.myAccountController = myAccountController;
	}

	public MapModel getEmptyModel() {
		return emptyModel;
	}

	public void addMessage(FacesMessage message) {
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the lat
	 */
	public double getLat() {
		return lat;
	}

	/**
	 * @param lat
	 *            the lat to set
	 */
	public void setLat(double lat) {
		this.lat = lat;
	}

	/**
	 * @return the lng
	 */
	public double getLng() {
		return lng;
	}

	/**
	 * @param lng
	 *            the lng to set
	 */
	public void setLng(double lng) {
		this.lng = lng;
	}

	/**
	 * @return the updateFlag
	 */
	public boolean isUpdateFlag() {
		return updateFlag;
	}

	/**
	 * @param updateFlag the updateFlag to set
	 */
	public void setUpdateFlag(boolean updateFlag) {
		this.updateFlag = updateFlag;
	}

	/**
	 * @return the latLang
	 */
	public String getLatLang() {
		return latLang;
	}

	/**
	 * @param latLang the latLang to set
	 */
	public void setLatLang(String latLang) {
		this.latLang = latLang;
	}

}
